package net.pulseproject.router.systemtest;

import net.pulseproject.commons.gui.ExceptionDialog;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;

import com.objogate.wl.swing.driver.JFrameDriver;

public final class DriverMatcher {
	
	private static final Log LOG = LogFactory.getLog(DriverMatcher.class);
	
	private DriverMatcher() {
		// utility class not instantiable
	}
	
    public static Matcher<JFrameDriver> hasAnyExceptionDialogDisplayed() {
		return new ExceptionDialogMatcher();
	}
    // TODO ExceptionDialogMatcher does not work :(
    private static class ExceptionDialogMatcher extends BaseMatcher<JFrameDriver> {
    	@Override
		public boolean matches(final Object object) {
			try {
				new ExceptionDialogDriver((JFrameDriver) object, ExceptionDialog.class).clickClose();
	    	} catch(final AssertionError error) {
	    		LOG.debug("AssertionError thrown while getting ExceptionDialog.");
	    		return false;
	    	}
	    	LOG.debug("ExceptionDialog could be found and clicked.");
	    	return true;
//	final ComponentFinder<JOptionPane> finder = this.all(JOptionPane.class, Matchers.anything());
//	final List<JOptionPane> panes = finder.components();
		}
		
		@Override
		public void describeTo(final Description description) {
			description.appendText("has any JOptionPanes displayed");
		}
    }
}
